package com.etc.controller;

import java.util.Random;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.etc.entity.Patientlogin;
import com.etc.message.Message;
import com.etc.service.PatientLoginService;
import com.etc.util.MD5Util;
@Controller
public class PatientRegController {
	
	    @Autowired
		private PatientLoginService patientLoginService;	    
		@RequestMapping(value="patientRegist",method=RequestMethod.POST)
		@ResponseBody
		public Patientlogin patientRegist(@RequestBody Patientlogin patientlogin,HttpSession session) {
		    
			String phone =patientlogin.getPatientloginid();			
			//System.out.println(phone);
			Patientlogin patients=patientLoginService.selPatientById(phone);
			if(patients==null) {
				Random random = new Random();
				//int code = random.nextInt(1000000);
				int code=778899;
				//把随机数存进session
				session.setAttribute("code", code);
				//调用短信验证接口
				Message m = new Message();
				//发送验证码至手机
				m.getRequest2(phone, String.valueOf(code));
				//System.out.println("短信");
				return patientlogin;
			}else {
				
				return null;
			}
			
		}
		
		@RequestMapping(value="Register",method=RequestMethod.POST)
		@ResponseBody
		public boolean addUsers1(String phone,String userPwd,String code,HttpSession session) {
		
			
			int  code1=0;
			if(session.getAttribute("code")!=null) {
				code1=(int) session.getAttribute("code");
			    System.out.println(code);
			    System.out.println(code1);
			} 
			if(Integer.valueOf(code)==code1) {
				
				Patientlogin patientlogin = new Patientlogin();
				userPwd=MD5Util.getEncodeByMd5(userPwd);
				patientlogin.setPatientloginid(phone);
				patientlogin.setUserpwd(userPwd);
			  	return patientLoginService.addPatient(patientlogin);
			  	
			}
			else {
			
			return false;
			}
			
		}
		/**
		 * 检查账号是否存在
		 * @param pl
		 * @param request
		 * @return
		 */
		@RequestMapping(value="checkAccount",method=RequestMethod.POST)
		@ResponseBody
		public String checkAccount(String phone,HttpServletRequest request) {						
			Patientlogin patientlogin=patientLoginService.selPatientById(phone);
			if(patientlogin==null) {
				return "0";
			}
			else 
				return "1";
			}
		/**
		 * 发送短信
		 * @return
		 */
		@RequestMapping(value="sendSMS",method=RequestMethod.POST)
		@ResponseBody
		public String sendSMS(String phone,HttpSession session,String code) {
			Patientlogin patientlogin=patientLoginService.selPatientById(phone);
			if(patientlogin==null) {
				Random random = new Random();
				int code1 = random.nextInt(1000000);
				//把随机数存进session
				session.setAttribute("code", code1);
				//调用短信验证接口
				Message m = new Message();
				//发送验证码至手机
				m.getRequest2(phone, String.valueOf(code1));
				return "0";
			}
			else 
				return "1";
			}
		}
			


	
